using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Web.Core.Objects;
using Web.Core;

public partial class AdminCP_AdminCP : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Membership"] != null)
        {
            Member mem = Session["Membership"] as Member;
            if (!mem.IsAdministrator() && !mem.IsEditor())
                Response.Redirect("Login.aspx");
            else
                AddLogs();
        }
        else
            Response.Redirect("Login.aspx");
    }

    void AddLogs()
    {
        Log l = new Log();
        l.LogID = 0;
        l.MemberID = (Session["Membership"] as Member).ID;
        l.IP = Request.Params["REMOTE_ADDR"];
        l.ComputerName = Request.Params["LOGON_USER"];
        l.Time = DateTime.Now.Month + "/" + DateTime.Now.Day + "/" + DateTime.Now.Year + " " + DateTime.Now.Hour + ":" + DateTime.Now.Minute + ":" + DateTime.Now.Second;
        l.URL = Request.Url.PathAndQuery;
        l.Action = GetAction();

        Logs.InsertLog(l);
    }

    string GetAction() // Khong lam 2 ngon ngu vi met + het thoi gian +___+
    {
        string action = "";
        switch (Utils.GetPageName(Request.Url.AbsolutePath).ToLower())
        {
            case "default":
                action = "Visit admincp homepage";
                break;
            case "products":
                if (Request.Params["do"] != null)
                {
                    switch (Request.Params["do"].ToString().ToLower())
                    {
                        case "edit":
                            action = "Editing product (ID = " + Request.Params["pid"].ToString() + ")";
                            break;
                        case "del":
                            action = "Delete product (ID = " + Request.Params["pid"].ToString() + ")";
                            break;
                        case "add":
                            action = "Adding product";
                            break;
                        default:
                            break;
                    }
                }
                else
                    action = "View list products";
                break;
            case "members":
                if (Request.Params["do"] != null)
                {
                    switch (Request.Params["do"].ToString().ToLower())
                    {
                        case "edit":
                            action = "Editing member (ID = " + Request.Params["mid"].ToString() + ")";
                            break;
                        case "del":
                            action = "Delete member (ID = " + Request.Params["mid"].ToString() + ")";
                            break;
                        case "add":
                            action = "Adding member";
                            break;
                        default:
                            break;
                    }
                }
                else
                    action = "View list members";
                break;
            case "categories":
                if (Request.Params["do"] != null)
                {
                    switch (Request.Params["do"].ToString().ToLower())
                    {
                        case "edit":
                            action = "Editing category (ID = " + Request.Params["cid"].ToString() + ")";
                            break;
                        case "del":
                            action = "Delete category (ID = " + Request.Params["cid"].ToString() + ")";
                            break;
                        case "add":
                            action = "Adding category";
                            break;
                        default:
                            break;
                    }
                }
                else
                    action = "View list categories";
                break;
            case "manufacturers":
                if (Request.Params["do"] != null)
                {
                    switch (Request.Params["do"].ToString().ToLower())
                    {
                        case "edit":
                            action = "Editing manufacturer (ID = " + Request.Params["mid"].ToString() + ")";
                            break;
                        case "del":
                            action = "Delete manufacturer (ID = " + Request.Params["mid"].ToString() + ")";
                            break;
                        case "add":
                            action = "Adding manufacturer";
                            break;
                        default:
                            break;
                    }
                }
                else
                    action = "View list manufacturers";
                break;
            case "advertisements":
                if (Request.Params["do"] != null)
                {
                    switch (Request.Params["do"].ToString().ToLower())
                    {
                        case "edit":
                            action = "Editing advertisement (ID = " + Request.Params["aid"].ToString() + ")";
                            break;
                        case "del":
                            action = "Delete advertisement (ID = " + Request.Params["aid"].ToString() + ")";
                            break;
                        case "add":
                            action = "Adding advertisement";
                            break;
                        default:
                            break;
                    }
                }
                else
                    action = "View list advertisements";
                break;
            case "orders":
                if (Request.Params["do"] != null)
                {
                    switch (Request.Params["do"].ToString().ToLower())
                    {
                        case "del":
                            action = "Delete order (ID = " + Request.Params["oid"].ToString() + ")";
                            break;
                        case "tocompleted":
                            action = "Update order (ID = " + Request.Params["oid"].ToString() + ")";
                            break;
                        default:
                            break;
                    }
                }
                else
                    action = "View list orders";
                break;
            case "contact":
                if (Request.Params["do"] != null)
                {
                    switch (Request.Params["do"].ToString().ToLower())
                    {
                        case "del":
                            action = "Delete contact (ID = " + Request.Params["cid"].ToString() + ")";
                            break;
                        default:
                            break;
                    }
                }
                else
                    action = "View list contacts";
                break;
            case "currencies":
                if (Request.Params["do"] != null)
                {
                    switch (Request.Params["do"].ToString().ToLower())
                    {
                        case "edit":
                            action = "Editing Currency (ID = " + Request.Params["cid"].ToString() + ")";
                            break;
                        case "del":
                            action = "Delete Currency (ID = " + Request.Params["cid"].ToString() + ")";
                            break;
                        case "add":
                            action = "Adding Currency";
                            break;
                        default:
                            break;
                    }
                }
                else
                    action = "View list Currencies";
                break;
            case "logs":
                action = "View logs";
                break;
            default:
                break;
        }
        return action;
    }
}
